﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.Results;
using System.Windows.Forms;
using log4net;
using Newtonsoft.Json;
using static CloudApiServer.Controllers.DemoController;

namespace CloudApiServer.Controllers
{
    /// <summary>
    /// 异常错误接口控制器
    /// </summary>
    public class ErrorController : ApiController
    {
        /// <summary>
        /// 未被定义的接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        [HttpPost]
        public IHttpActionResult NotFound(HttpRequestMessage request)
        {

            string AbsolutePath = request.RequestUri.AbsolutePath.ToString();
            string url = request.RequestUri.ToString();
            if (AbsolutePath == "/")
            {
                return Ok($"当前请求的地址为接口服务地址，请通过客户端或web应用请求！当前控制台版本号：{SystemInfo.Version} 编译时间：{SystemInfo.Versiontime}");
            }
            else
            {
                string Method = request.Method.ToString();
                Console.WriteLine($"调用不存在的接口!请求接口{url}\r\n请求入参{request.Content.ReadAsStringAsync().Result}");
                return Json(new { code = 404, msg = $"请求的路径【{url}】方法【{Method}】不存在！请检查传入的地址是否有误或联系管理员！", data = "" });
            }

        }
    }
}
